//
//  MainTabBar.swift
//  SinaWeiBo
//
//  Created by dengchen on 15/11/8.
//  Copyright (c) 2015年 name. All rights reserved.
//

import UIKit




class MainTabBar: UITabBar {
    

    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(composeBtn)

    }
    
    //如果重写 init(frame:) 系统会默认为这个控件是通过代码创建
        //只有默认报错的语句
        //如果有调用者通过 sb / xib创建了该对象 这个时候程序就崩溃
    
    required init?(coder aDecoder: NSCoder) {
//        fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        addSubview(composeBtn)
    }
    

    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let w = self.bounds.width/5
        let h = self.bounds.height
        let rect = CGRect(x: 0, y: 0, width: w, height: h)
        
       var index:CGFloat = 0
        
        
      for subview in subviews {
        
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!){
            
                subview.frame = CGRectOffset(rect,index * w, 0)

                index += index == 1 ? 2 : 1 //如果index==1 就自增2,否则自增1
            }
        }
        composeBtn.frame = CGRectOffset(rect, w * 2,0)
        bringSubviewToFront(composeBtn)
    }
    
    

    internal let composeBtn: UIButton = {
       
        let btn = UIButton()
        
        btn.layer.cornerRadius = 10
        btn.layer.masksToBounds = true
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        btn.sizeToFit()
        
        return btn
        
    }()
    
    
    

    
}
